package cn.edu.zut.pms.faker;

import com.github.javafaker.Faker;
import java.sql.*;
import java.util.Locale;
import java.util.Random;

public class FakerLogisticsDistrictGoodsStatus {
    public static void main(String[] args) {
        // 数据库连接信息（请根据实际环境修改）
        String url = "jdbc:mysql://192.168.17.150:3306/soil?allowPublicKeyRetrieval=true&useSSL=false";
        String user = "root";
        String password = "000000";

        // 创建中文Faker实例
        Faker faker = new Faker(new Locale("zh_CN"));
        Random random = new Random();

        // 定义销售区域（与历史表保持一致的区域划分）
        String[] salesRegions = {"华北区", "华东区", "华南区", "西北区", "西南区", "东北区", "华中区"};

        try (Connection connection = DriverManager.getConnection(url, user, password)) {
            // 先清空表中现有数据（可选）
            String truncateSql = "TRUNCATE TABLE ads_logistics_district_goods_status";
            try (Statement statement = connection.createStatement()) {
                statement.execute(truncateSql);
            }

            // 插入数据的SQL语句
            String insertSql = "INSERT INTO ads_logistics_district_goods_status (" +
                    "sales_region, late_cnt, ontime_cnt" +
                    ") VALUES (?, ?, ?)";

            try (PreparedStatement preparedStatement = connection.prepareStatement(insertSql)) {
                for (String region : salesRegions) {
                    // 为每个区域生成总订单量（基础量）
                    int totalOrders;
                    switch (region) {
                        case "华东区":
                        case "华南区":
                            totalOrders = faker.number().numberBetween(8000, 15000); // 订单量较大
                            break;
                        case "华北区":
                        case "华中区":
                            totalOrders = faker.number().numberBetween(5000, 10000);
                            break;
                        default:
                            totalOrders = faker.number().numberBetween(3000, 7000); // 订单量相对较少
                    }

                    // 生成准时率（发达区域准时率更高）
                    double ontimeRate;
                    switch (region) {
                        case "华东区":
                        case "华南区":
                            ontimeRate = faker.number().randomDouble(2, 0, 0); // 85%-95%准时率
                            break;
                        case "华北区":
                        case "华中区":
                            ontimeRate = faker.number().randomDouble(2, 0, 0);
                            break;
                        default:
                            ontimeRate = faker.number().randomDouble(2, 0, 0); // 偏远区域准时率较低
                    }

                    // 计算准时和延迟数量（确保整数且总和等于总订单量）
                    int ontimeCnt = (int) Math.round(totalOrders * ontimeRate);
                    int lateCnt = totalOrders - ontimeCnt;

                    // 避免出现0或负数（边界处理）
                    ontimeCnt = Math.max(1, ontimeCnt);
                    lateCnt = Math.max(0, lateCnt);

                    // 设置参数
                    preparedStatement.setString(1, region);
                    preparedStatement.setInt(2, lateCnt);
                    preparedStatement.setInt(3, ontimeCnt);

                    preparedStatement.executeUpdate();
                }
                System.out.println("数据插入完成，共插入 " + salesRegions.length + " 条区域交货状态记录");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}